package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        // 1.从缓存中获取护理项目
        NursingProject nursingProject = redisCache.getCacheObject("nursingProject" + id);
        // 2.如果缓存中没有数据，则从数据库中查询
        if (nursingProject == null) {
            nursingProject = nursingProjectMapper.selectById(id);
            // 3.将查询结果缓存到Redis中
            try {
                redisCache.setCacheObject("nursingProject" + id, nursingProject);
            } catch (Exception e) {
                log.error("查询护理项目缓存失败");
            }
        }
        // 4.返回结果
        return nursingProject;
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        List<NursingProject> nursingProjectList = nursingProjectMapper.selectNursingProjectList(nursingProject);
        try {
            redisCache.setCacheList("nursingProjectList", nursingProjectList);
        } catch (Exception e) {
            log.error("查询护理项目列表缓存失败");
        }
        // 2.返回结果
        return nursingProjectList;
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        nursingProject.setCreateTime(DateUtils.getNowDate());
        // 1.新增护理项目
        int rows = nursingProjectMapper.insert(nursingProject);
        // 2.删除缓存
        try {
            redisCache.deleteObject("nursingProjectList");
        } catch (Exception e) {
            log.error("新增护理项目后删除缓存失败");
        }
        // 3.返回结果
        return rows > 0 ? 1 : 0;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        nursingProject.setUpdateTime(DateUtils.getNowDate());
        // 1.修改护理项目
        int rows = nursingProjectMapper.updateById(nursingProject);
        // 2.删除缓存
        try {
            redisCache.deleteObject("nursingProjectList");
        } catch (Exception e) {
            log.error("修改护理项目后删除缓存失败");
        }
        // 3.返回结果
        return rows > 0 ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        // 1.删除护理项目
        int rows = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
        // 2.删除缓存
        try {
            redisCache.deleteObject("nursingProjectList");
        } catch (Exception e) {
            log.error("批量删除护理项目后删除缓存失败");
        }
        // 3.返回结果
        return rows > 0 ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        // 1.删除护理项目
        int rows = nursingProjectMapper.deleteById(id);
        // 2.删除缓存
        try {
            redisCache.deleteObject("nursingProjectList");
        } catch (Exception e) {
            log.error("删除护理项目后删除缓存失败");
        }
        // 3.返回结果
        return rows > 0 ? 1 : 0;
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        // 1.从缓存中获取护理项目VO列表
        List<NursingProjectVo> nursingProjectVoList = redisCache.getCacheObject("nursingProjectVoList");
        // 2.如果缓存中没有数据，则从数据库中查询
        if (nursingProjectVoList == null) {
            nursingProjectVoList = nursingProjectMapper.getAllProjects();
            // 3.将查询结果缓存到Redis中
            try {
                redisCache.setCacheObject("nursingProjectVoList", nursingProjectVoList);
            } catch (Exception e) {
                log.error("查询护理项目VO列表缓存失败");
            }
        }
        // 4.返回结果
        return nursingProjectVoList;
    }
}
